package com.changgou.goods.controller;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.SpuService;
import com.changgou.pojo.Goods;
import com.changgou.pojo.Spu;
import com.github.pagehelper.Page;
import org.hibernate.validator.constraints.ParameterScriptAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/spu")
public class SpuController {


    @Autowired
    private SpuService spuService;

    /*逻辑删除*/
    @PutMapping("/delete/{id}")
    public Result delete(@PathVariable("id") String id) {
        spuService.deletelogic(id);
        return new Result(true, StatusCode.OK, "逻辑删除成功");

    }

    /*逻辑恢复*/
    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable("id") String id) {
        spuService.restore(id);
        return new Result(true, StatusCode.OK, "逻辑恢复成功");

    }
    /*物理删除*/
    @DeleteMapping("/realDelete/{id}")
    public Result realDelete(@PathVariable("id") String id) {
        spuService.delete(id);
        return new Result(true, StatusCode.OK, "物理擅长成功");

    }

    /*上架商品，需要审核通过的商品*/
    @PutMapping("/put/{id}")
    public Result put(@PathVariable("id") String id) {

        spuService.put(id);
        return new Result(true, StatusCode.OK, "上架完成");
    }


    /*下架商品，需要校验是否是被删除的商品，如果未删除则修改上架状态为0*/
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable("id") String id) {
        spuService.pull(id);
        return new Result(true, StatusCode.OK, "下架完成");

    }

    /*审核商品，需要校验是否是被删除的商品，如果未删除则修改审核状态为1，并自动上架*/
    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable("id") String id) {
        spuService.audit(id);
        return new Result(true, StatusCode.OK, "审核完成，已上架");

    }


    @PutMapping("/edit/{spuId}")
    public Result update(@RequestBody Goods goods) {

        spuService.update(goods);
        return new Result(true, StatusCode.OK, "成功");

    }

    @PostMapping("/add")
    public Result add(@RequestBody Goods goods) {

        spuService.add(goods);
        return new Result(true, StatusCode.OK, "保存成功");

    }

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Spu> spuList = spuService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",spuList) ;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/findSpuById/{id}")
    public Spu findSpuById(@PathVariable String id){
        return spuService.findById(id);
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        Spu spu = spuService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",spu);
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List<Spu> list = spuService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Spu> pageList = spuService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }


}
